﻿using ajayumi.develop.win32.Transport.WCF.Services.DataContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ajayumi.develop.win32.Transport.AppUpgrade
{
    static class Program
    {
        internal static UpgradeInfo UpgradeInfo { get; set; }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            ArgumentSerialization.Load(args);

            if (ArgumentSerialization.IsConfirmCheckVersion)
            {
                try
                {
                    using (UpgradeServiceClient client = new UpgradeServiceClient())
                    {
                        CheckUpgradeVerInfo cuvi = client.CheckVersion(ArgumentSerialization.AppName, ArgumentSerialization.AppVersion);

                        if (cuvi.HasUpgrade)
                        {
                            string msg = "发现有新版本，是否升级？";
                            if (cuvi.IsNeed)
                            {
                                msg = "发现有新版本，该版本做了稳定性改进，请升级！";
                            }
                            using (ConfirmCheckDlg dlg = new ConfirmCheckDlg())
                            {
                                if (dlg.ShowDialog(msg, cuvi.IsNeed) == System.Windows.Forms.DialogResult.Yes)
                                {
                                    ShowWizardForm();
                                }
                            }
                            //if (MessageBox.Show("有新版本，是否升级？", "升级",
                            //    MessageBoxButtons.YesNo,
                            //    MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                            //{
                            //    ShowWizardForm();
                            //}
                        }
                    }
                }
                catch (Exception ex)
                { }
                finally
                {
                    System.Environment.Exit(System.Environment.ExitCode);
                }
            }
            else
            {
                ShowWizardForm();
            }
        }


        /// <summary>
        /// 显示向导窗口
        /// </summary>
        static void ShowWizardForm()
        {
            WizardForm frm = new WizardForm();
            frm.Text = string.Format("{0}（当前版本：{1}）", frm.Text, ArgumentSerialization.AppVersion);
            Application.Run(frm);
        }


    }
}
